
package com.xiaoma.myaudience.biz.task;

import java.io.File;

import org.apache.http.client.methods.HttpGet;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;

import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.util.HttpUtils;
import com.xiaoma.myaudience.util.Logger;
import com.xiaoma.myaudience.util.Pair;
import com.xiaoma.myaudience.util.PicUtils;
import com.xiaoma.myaudience.util.StringUtils;
import com.xiaoma.myaudience.util.http.AndroidHttpClient;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask;

/**
 * 异步加载图片.
 * 
 * @author wjying
 */
public class ImageLoadingTask extends BaseDataAsyncTask<String, Void, Pair<String[], Bitmap>> {
    private final static String TAG = "ImageLoadingTask";

    // private final static Object sLock = new Object();
    private static boolean mIsHead;
    
    public ImageLoadingTask(Context context, int taskType, DataAsyncCallback callback, boolean isHead) {
    	super(context, taskType, callback);
    	mIsHead = isHead;
    }

    public ImageLoadingTask(Context context, int taskType, DataAsyncCallback callback) {
        super(context, taskType, callback);

    }

    @Override
    protected Pair<String[], Bitmap> doInBackground(String... params) {
        // synchronized (sLock) {
        String path = params[0];
        String size = params.length > 1 ? params[1] : null;
        try {
            return getBitmap(mContext, getHttpClient(), path, size, true);
        } finally {
            closeHttpClient();
        }

        // }
    }

    /**
     * 执行取图片
     * 
     * @param context
     * @param httpClient
     * @param path
     * @param size
     * @return
     */
    public static Pair<String[], Bitmap> getBitmap(Context context, AndroidHttpClient httpClient,
            String path, String size, boolean needReturn) {
        String[] ret = new String[] {path, size};
        try {
            if (!TextUtils.isEmpty(path)) {
                Bitmap bitmap = null;
                File file = getLocalFile(path);
                if (file == null) {
                    // 读取图片
                	if (mIsHead) {
                		bitmap = HttpUtils.getHttpBitmapResult(httpClient, Constant.IMG_URL_HEAD
                				+ path, null, null, HttpGet.METHOD_NAME, null, context.getResources());
					} else {
						bitmap = HttpUtils.getHttpBitmapResult(httpClient, Constant.IMG_URL_PREFEX
								+ path, null, null, HttpGet.METHOD_NAME, null, context.getResources());
					}
                    // 保存到本地
                    Bitmap saveBitmap = PicUtils.saveBitmap(bitmap, Constant.FILE_TMP_IMG
                            + StringUtils.getNameFromPath(path), size);
                    if (bitmap != null && saveBitmap != bitmap) {
                        bitmap.recycle();
                        bitmap = null;
                    }

                    return Pair.create(ret, saveBitmap);
                } else {
                    bitmap = PicUtils.readBitmap(context.getResources(), file);
                    if (bitmap == null) {
                        // 可能下载图片有错误,删除
                        file.delete();
                    }
                }

                if (!needReturn && bitmap != null) {
                    bitmap.recycle();
                    bitmap = null;
                }

                return Pair.create(ret, bitmap);
            }
        } catch (Exception e) {
            Logger.e(TAG, "doInBackground error!!!", e);
        }

        return Pair.create(ret, null);
    }

    /**
     * 检查某个url的图片是否存在. 存在返回File,否则返回null
     * 
     * @param url
     * @return
     */
    private static File getLocalFile(String url) {
        File file = new File(Constant.FILE_TMP_IMG + StringUtils.getNameFromPath(url));
        return file.exists() ? file : null;
    }
}
